package com.huiquan.vocab.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.constants.AnalysisSentenceConstant;
import com.huiquan.framework.base.BaseService;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.GetListUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.vocab.constant.VocabConstant;
import com.huiquan.vocab.dao.VocabElementSourceDao;
import com.huiquan.vocab.domain.VocabElementRestrictSource;
import com.huiquan.vocab.domain.VocabElementSource;
import com.huiquan.vocab.utils.VocabUtils;

@Service
public class VocabElementSourceService extends BaseService {

	@Autowired
	private VocabElementSourceDao vocabElementSourceDao;

	public ReturnData elementChangeRestrictSource(String id, String restrictSource) {

		// 获取数据库中该来源对应的限定来源，判断是删除还是添加
		String delFlag = "";
		VocabElementSource source = vocabElementSourceDao.retrieveObjectById(Long.parseLong(id));
		String rs = source.getRestrictSource();
		if (rs.indexOf(restrictSource + ",") >= 0) {
			rs = rs.replace(restrictSource + ",", "");
			delFlag = "1";
		} else {
			rs += restrictSource + ",";
			delFlag = "0";
		}

		// 修改到数据库
		VocabElementRestrictSource ers = vocabElementSourceDao.retrieveRestrictSourceByElementId(source.getElementId());
		if(ers == null) {
			ers = new VocabElementRestrictSource();
			ers.setElementId(source.getElementId());
			ers.setRestrictSource(rs);
			
			vocabElementSourceDao.insertRestrictSource(ers);
		} else {
			vocabElementSourceDao.updateRestrictSource(source.getElementId(), rs);
		}

		Map<String, Object> map = new HashMap<>();
		map.put("delFlag", delFlag);
		return ReturnUtil.success(map);
	}

	public ModelAndView list(String startStr, String vocabularyKey, Integer checkFlag, String exactSearchFlag,
			String countPerPageStr, String xbsSourceKey, String propertyKey) {
		boolean exactFlag = exactSearchFlag != null && exactSearchFlag.equals("1");

		Map<String, Object> param = new HashMap<>();
		if (vocabularyKey != null && !vocabularyKey.isEmpty()) {
			param.put("vocabulary", exactFlag ? vocabularyKey : "%" + vocabularyKey + "%");
		}
		if (propertyKey != null && !propertyKey.isEmpty()) {
			param.put("property", propertyKey);
		}
		param.put("checkFlag", checkFlag);
		param.put("source", xbsSourceKey);

		int totalSize = vocabElementSourceDao.retrieveSize(param);

		Map<String, Integer> pageNo;
		if (countPerPageStr != null && !countPerPageStr.isEmpty()) {
			int countPerPage = Integer.parseInt(countPerPageStr);
			pageNo = GetListUtil.getPageNoParam(totalSize, startStr, countPerPage);
			param.put("countPerPage", countPerPage);
		} else {
			pageNo = GetListUtil.getPageNoParam(totalSize, startStr);
		}
		int endPage = pageNo.get("endPage");
		int start = pageNo.get("start");
		int startIndex = pageNo.get("startIndex");

		List<VocabElementSource> list = new ArrayList<>();

		if (startIndex < totalSize) {
			param.put("startIndex", startIndex);
			param.put("orderStr", "replace_vocab");
			list = vocabElementSourceDao.retrieveList(param);

			for (VocabElementSource vocabElement : list) {
				String[] sentence = VocabUtils.getSentenceAndShowSentence(vocabElement.getSentence());
				vocabElement.setSentence(sentence[0]);
			}
		}

		Map<String, Object> map = GetListUtil.getReturnMap2(totalSize, start, startIndex, endPage, list, null);

		Map<String, String> showSearch = new LinkedHashMap<>();
		showSearch.put("vocabularyKey", "词语");
		map.put("showSearch", showSearch);
		map.put("xbsSourceMap", AnalysisSentenceConstant.XBS_SOURCE_TYPE_SIMPLE_TITLE);
		map.put("propertyList", VocabConstant.elementProperty);

		return new ModelAndView("vocab/element_index", map);
	}

	public ReturnData submitPage(String ids) {

		vocabElementSourceDao.checkByIds(ids);

		return ReturnUtil.success();
	}

	public ReturnData init() {
		
		vocabElementSourceDao.init();
		
		return ReturnUtil.success();
	}
}
